<template>
	<view class="content">
		<view class="form-wrap">
			<view class="form-item">
				<image class="icon" src="/static/img/phone_icon.png" mode=""></image>
				<input v-model="tel" type="number" placeholder="请输入手机号码">
			</view>
			<view class="form-item">
				<image class="icon" src="/static/img/yanzhengma_icon.png" mode=""></image>
				<input v-model="code" type="number" placeholder="请输入验证码">
				<button :disabled="disabled" @click="sendSMS" :class="['sendMessage-btn', disabled ? 'disabled' : '']" type="primary">{{verifyCodeBtnText}}</button>
			</view>
		</view>
		<view class="btn-wrap">
			<button type="primary" @click="submit">提交</button>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				userInfo: {},
				tel: '',
				code: '',
				disabled: false,
				verifyCodeBtnText: '获取验证码',
			}
		},
		onLoad(options) {
			this.userInfo = JSON.parse(options.userinfo)
		},
		onReady() {
			
		},
		methods: {
			ctimer: function (time) {
				var t = time;
				if (t > 0) {
					this.disabled = true;
					this.verifyCodeBtnText = t + '秒';
					t--;
					setTimeout(() => {
						this.ctimer(t);
					}, 1000);
				} else {
					this.disabled = false;
					this.verifyCodeBtnText = '获取验证码';
				}
			},

			sendSMS: function () {
				var regex = /^(?=\d{11}$)^1(?:3\d|4[57]|5[^4\D]|66|7[^249\D]|8\d|9[189])\d{8}$/g;
				if (this.tel == '') {
					uni.showToast({
						title: '请输入手机号码',
						position: 'bottom',
						icon: 'none'
					});
					return false;
				}
				if (!regex.test(this.tel)) {
					uni.showToast({
						title: '请输入正确的手机号码',
						position: 'bottom',
						icon: 'none'
					});
					return false;
				}
				uni.showLoading({
					title: ''
				});
				this.$api.api.ajaxData({
					method: 'V1.SendMassage',
					tel: this.tel,
				}, res => {
					uni.hideLoading();
					uni.showToast({
						title: res.msg,
						position: 'bottom',
						icon: 'none'
					});
					if (res.status) {
						this.ctimer(60);
					}
				})
			},

			submit: function () {
				var regex = /^(?=\d{11}$)^1(?:3\d|4[57]|5[^4\D]|66|7[^249\D]|8\d|9[89])\d{8}$/g;
				if (this.tel == '') {
					uni.showToast({
						title: '请输入手机号码',
						position: 'bottom',
						icon: 'none'
					});
					return false;
				}
				if (!regex.test(this.tel)) {
					uni.showToast({
						title: '请输入正确的手机号码',
						position: 'bottom',
						icon: 'none'
					});
					return false;
				}
				if (this.code == '') {
					uni.showToast({
						title: '请输入验证码',
						position: 'bottom',
						icon: 'none'
					});
					return false;
				}
				uni.showLoading({
					title: '',
				});
				this.$api.api.ajaxData({
					method: 'V1.UserAuth',
					uid: this.userInfo.id,
					token: this.userInfo.token,
					type: '1',
					tel: this.tel,
					code: this.code,
				}, res => {
					uni.hideLoading();
					uni.showToast({
						title: res.msg,
						position: 'bottom',
						icon: 'none'
					});
					if (res.status) {
						uni.$emit('my-page', {
							type: 'update',
						})
						uni.navigateBack({
							delta: 1
						})
					}
				})
			},

		}
	}
</script>

<style lang="scss">
	@import '../../common/common.scss';
	.form-wrap {
		padding: 5rpx 0;

		.form-item {
			display: flex;
			justify-content: flex-start;
			align-items: center;
			background: #FFFFFF;
			padding: 0 50rpx;
			& + .form-item {
				border-top: 1rpx solid $border-light-color;
			}

			.icon {
				width: 40rpx;
				height: 40rpx;
			}

			input {
				flex: 1;
				height: 100rpx;
				line-height: 100rpx;
				text-indent: 20rpx;
				font-size: 24rpx;
			}

			.sendMessage-btn {
				min-width: 200rpx;
				height: 80rpx;
				line-height: 80rpx;
				border-radius: 80rpx;
				font-size: 28rpx;
			}
		}
	}

	.btn-wrap {
		margin-top: 100rpx;
		padding: 0 100rpx;

		button {
			border-radius: 100rpx;
		}
	}
</style>
